//
//  WalletViewController.m
//  PeopleStreet
//
//  Created by 刘建峰 on 16/6/6.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "WalletViewController.h"
#import "RetrievePasswordViewController.h"
#import "AntCurrencyDetailViewController.h"

@interface WalletViewController ()

@property (weak, nonatomic) IBOutlet UIScrollView *scroView;
@property (weak, nonatomic) IBOutlet UILabel *leftMoney;
@property (weak, nonatomic) IBOutlet UIButton *addMoneyButton;
@property(nonatomic,assign)NSInteger ifHadPayPwd;
@property (strong, nonatomic) IBOutlet UILabel *goldLabel;
@property (strong, nonatomic) IBOutlet UILabel *silverLabel;

@end

@implementation WalletViewController

- (void)viewWillAppear:(BOOL)animated{
    [self ifHadPayPassword];
    [self getPersonalLeftMoney];
    [self getGoldAndSilver];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initViewLayout];
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.scroView];
    
    _scroView.contentSize = flexibleSize(CGSizeMake(0, 569), NO);
    _addMoneyButton.layer.cornerRadius = 18*VerticalRatio();
}

- (IBAction)back:(id)sender {
    [self.navigationController  popViewControllerAnimated:YES];
}

#pragma mark == 蚂蚁币明细
- (IBAction)detail:(id)sender {
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
    AntCurrencyDetailViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"AntCurrencyDetail"];
    page.gold = _goldLabel.text;
    page.silver = _silverLabel.text;
    [self.navigationController  pushViewController:page animated:YES];
}

#pragma mark == 密码管理
- (IBAction)controlPassword:(id)sender {
//    if (_ifHadPayPwd<=0) {
//        UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
//        RetrievePasswordViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"RetrievePassword"];
//        page.titleStr = @"设置支付密码";
//        [self.navigationController  pushViewController:page animated:YES];
//    }else{
//        UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
//        UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"PasswordManagement"];
//        [self.navigationController  pushViewController:page animated:YES];
//    }
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
    UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"PasswordManagement"];
    [self.navigationController  pushViewController:page animated:YES];
}

#pragma mark   ===  查询用户是否已经设置支付密码
- (void)ifHadPayPassword{
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_hasPayPwd withHttpType:@"POST" withTag:SX_SX_hasPayPwd];
}

#pragma mark == 请求个人钱包余额
- (void)getPersonalLeftMoney{
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_getEpurse withHttpType:@"POST" withTag:SX_SX_getEpurse];
}
#pragma mark == 查询可用金银币
- (void)getGoldAndSilver{
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    self.shareApi.delegate=self;
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"msceMemberId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [self.shareApi  requestOperationWithDic:dic withPath:SX_getMemberEpurse withHttpType:@"POST" withTag:SX_SX_getMemberEpurse];
}

#pragma mark == 账单记录
- (IBAction)saleHistory:(id)sender {
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
    UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"AddMoneyHistory"];
    [self.navigationController  pushViewController:page animated:YES];
}

#pragma mark == 蚂蚁币介绍
- (IBAction)jieshao:(id)sender {
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
    UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"AntCurrencyIntroduce"];
    [self.navigationController  pushViewController:page animated:YES];
}

#pragma mark == //充值
- (IBAction)addMoney:(id)sender {
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
    UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"AddMoney"];
    [self.navigationController  pushViewController:page animated:YES];

}

#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    if (tag == SX_SX_hasPayPwd){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            _ifHadPayPwd = [database[@"result"][@"flag"]integerValue];
            
        }
    }else if (tag == SX_SX_getEpurse){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            NSString * money = [NSString stringWithFormat:@"%.2f",[database[@"result"][@"epurse"]floatValue]];
            NSMutableAttributedString * str = [[NSMutableAttributedString alloc]initWithString:money];
            [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:26*VerticalRatio()] range:NSMakeRange(money.length-3, 3)];
            _leftMoney.attributedText = str;
        }
        
    } else if (tag == SX_SX_getMemberEpurse){
        if ([[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            NSDictionary *dic = database[@"result"];
            if (![database[@"result"] isEqual:[NSNull null]]) {
                if ([dic.allKeys containsObject:@"mepuEpurse"]) {
                    if ([dic[@"mepuEpurse"] isEqual:[NSNull null]]) {
                        _goldLabel.text = @"0";
                    } else {
                        _goldLabel.text = [NSString stringWithFormat:@"%@",dic[@"mepuEpurse"]];
                    }
                } else {
                    _goldLabel.text = @"0";
                }
                if ([dic.allKeys containsObject:@"mepuSilver"]) {
                    if ([dic[@"mepuSilver"] isEqual:[NSNull null]]) {
                        _silverLabel.text = @"0";
                    } else {
                        _silverLabel.text = [NSString stringWithFormat:@"%@",dic[@"mepuSilver"]];
                    }
                } else {
                    _silverLabel.text = @"0";
                }
            } else {
                _goldLabel.text = @"0";
                _silverLabel.text = @"0";
            }
        }
        
    }

}
-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    
    //
    [self dismissWaitDialog];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
